package com.example.controller;

import com.example.pojo.UserInfo;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/toRegistration")
    public String toRegistration(){
        return "registration";
    }

    /**
     * @ResponseBody :标注在方法上，此时返回值不再是视图，
     * 而是数据，会将数据放入响应体中
     * 异步中的数据接收  参考  同步请求
     */
    @GetMapping("/validateEmail")
    @ResponseBody
    public String validateEmail(String email){
        System.out.println("email = " + email);
        boolean flag = userService.getInfoByEmail(email);
        if (flag){
            return "fail";
        }
        return "success";
    }

    /**
     * post方式的异步，请求参数是在请求体中，此时需要从请求体中获取数据
     * @RequestBody :从请求体中获取数据
     */
    @PostMapping("/validateEmail")
    @ResponseBody
    public String validateEmail2(@RequestBody UserInfo userInfo){
        System.out.println("email = " + userInfo.getEmail());
        boolean flag = userService.getInfoByEmail(userInfo.getEmail());
        if (flag){
            return "fail";
        }
        return "success";
    }
    //异步中，返回map，默认会解析成json对象   list：json数组   返回po：json对象
    @PostMapping("/ajax")
    @ResponseBody
    public Map testAjax(){
        Map<String,Object> map = new HashMap<>();
        map.put("msg","success");
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("jack");
        userInfo.setEmail("5456@qq.com");
        map.put("info",userInfo);
        List<Integer> list = Arrays.asList(1,8,9,5,7,56,556,888);
        map.put("list",list);
        return map;
    }


}
